package server;

import java.io.File;
import servlet.*;

public class ServletServer {
	
	private static ServletServer m_instantce = null;
	private JspCompiler compiler;
	private MyClassLoader classLoader;
	
	private ServletServer(){
		this.compiler = new DefaultJspCompiler();
		this.classLoader = new  MyClassLoader();
	};
	//单例模式获取ServletServer实例
	synchronized public static ServletServer getInstance(){
		if(m_instantce==null){
			m_instantce = new ServletServer();
		}
		return m_instantce;
	}
	
	public JspCompiler getCompiler() {
		return compiler;
	}
	//策略模式，接受实现JspCompiler接口的JSP编译器
	public void setCompiler(JspCompiler compiler) {
		this.compiler = compiler;
	}
	//ServletServer的处理方法
	public void process(MyRequest request, MyResponse response){

		MyServlet servlet;
		//如果请求资源为Servlet
		if(request.getRequestResourceType().equals("servlet")){
			//加载Servlet类
			Class<MyServlet> servletClass = (Class<MyServlet>)classLoader.loadServlet(request);
			if(servletClass!=null){
				try {
					//类反射方法创建Servlet实例，并执行其service方法
					servlet = servletClass.newInstance();
					servlet.service(request, response);
					response.setStatus(200);
					response.setContentType("text/html");
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			else{
				response.setStatus(404);
				response.setContentType("text/html");
				response.setMessage("404 Not Found");
			}			
		}
		else if(request.getRequestResourceType().equals("jsp")){
			File jspFile = new File(WebServer.getServerContext().getServerRootPath()+"/webapps"+request.getRequestResourcePath());
			if(jspFile.exists()){
				String dest = WebServer.getServerContext().getServerRootPath()+"/work/MyServer/"+request.getProjectName()+"/pku/ss/jsp";
				dest += request.getRequestResourcePath().substring(request.getRequestResourcePath().indexOf("/", 1), request.getRequestResourcePath().lastIndexOf("/"));
				File destPath = new File(dest);
				if(!destPath.exists()){
					destPath.mkdirs();
				}
				File jsp_servlet = new File(dest+"/"+request.getRequestResourceName().substring(0, request.getRequestResourceName().length()-3)+"class");
				//如果JSP还未编译，或者编译后JSP有修改，则需要将JSP进行编译，生成一个Servlet的class文件
				if(!jsp_servlet.exists()||jsp_servlet.lastModified()<jspFile.lastModified()){
					//compile
					String packages = request.getRequestResourcePath().substring(request.getRequestResourcePath().indexOf("/", 1)+1, request.getRequestResourcePath().lastIndexOf("/")).replace("/", ".");
					System.out.println(packages);
					String classPath = WebServer.getServerContext().getServerRootPath()+"/webapps/"+request.getProjectName()+"/WEB-INF/lib/*.jar";
					compiler.compile(jspFile, destPath,packages,classPath);
	
				}
				//执行与Servlet相同的步骤进行加载，实例化，执行service方法
				Class<MyServlet> servletClass = (Class<MyServlet>)classLoader.loadJspServlet(request);
				if(servletClass!=null){
					try {
						servlet = servletClass.newInstance();
						response.setCharSet("utf-8");
						servlet.service(request, response);
						response.getWriter().print("123123123");
						response.setStatus(200);
						response.setContentType("text/html");
					} catch (InstantiationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				else{
					response.setStatus(404);
					response.setContentType("text/html");
					response.setMessage("404 Not Found");
				}			
			}
			else{
				response.setStatus(404);
				response.setContentType("text/html");
				response.setMessage("404 Not Found");
			}
			
		}
		else{
			//response.setError();
		}
		
	}
}
